<template>
	<om-formsub-outer :index="index"><om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform></om-formsub-outer>
</template>

<script>
import { subOnLoad, changeEditor, configSelectData } from '../../../static/js/formUtil.js';
import { ignoreHtml, ptFloat, minusF, getDateStr,confirmModal, joinObjStr } from '@/static/js/common.js';
import urlUtil from '@/static/js/urlUtil.js'
export default {
	provide() {
		return {
			formPage: this
		};
	},
	data() {
		return {
			initValue: {},
			autoformdata: [],
			cachename: '',
			index: null,
			datas: [],
			order: 0,
			formdata: {},
			relevantAbilityCacheKey: 'hr-train-instYearPlan-title1-relevantAbility-cache',
			postCacheKey: 'hr-train-instYearPlan-title1-post-cache',
			monthCacheKey:"hr-train-instYearPlan-title1-month-cache",
			monthCacheKey1:"hr-train-instYearPlan-title1-month1-cache",
			projectCacheKey:'hr-train-instYearPlan-title1-project-cache',
			evaluateUserPostCacheKey:"hr-train-instYearPlan-title1-evaluateUserPost-cache",
			teacherCacheKey:"hr-train-instYearPlan-title1-teacher-cache"
		};
	},
	methods: {
		verifyTeacher(id,name){
			let _that = this
			_that.om.request(
				"trainInstYearPlan/verifyTeacher",
				{
					userId:id,
					relevantAbilityIdStr:''
				},
				'POST'
			).then(res=>{
				if(!res.value.flag){
					confirmModal(_that,'提示信息',res.value.confirmMsg,()=>{
						_that.$refs.autoform.formdata.teacherId = id
						_that.$refs.autoform.formdata.teacherName = name
					},()=>{
						
					})
				}
			})
		},
		getMonthList(){
			let _that  = this
			_that.om.request(
				"dictZero/getComboTree",
				{
					code:"38283"
				}
			).then(res=>{
				let monthList = []
				for(let item of res){
					let obj = JSON.parse(JSON.stringify(item))
					delete obj.children
					monthList.push(obj)
					if(item.children){
						for(let sub of item.children){
							let obj1 = JSON.parse(JSON.stringify(sub))
							delete obj1.children
							monthList.push(obj1)
						}
					}
				}
				_that.$refs.autoform.changeEditor("title2","startDateObjId",{
					link:"../../public/pageSearch/pageSearch",
					"linkParam":{
						other:JSON.stringify({
						}),
						url:"dictZero/getComboTree",
						namefield:"text",
						defaultData:JSON.stringify(monthList),
						initload:true,
						cachename:_that.monthCacheKey,
					}
				})
				_that.$refs.autoform.changeEditor("title2","endDateObjId",{
					link:"../../public/pageSearch/pageSearch",
					"linkParam":{
						other:JSON.stringify({
						}),
						url:"dictZero/getComboTree",
						namefield:"text",
						defaultData:JSON.stringify(monthList),
						initload:true,
						cachename:_that.monthCacheKey1,
					}
				})
				
				
			})
		},
		initLoad() {
			let _that = this;
			changeEditor(_that.autoformdata[0].editors,"teacherId","isSearchUser",true)
			changeEditor(_that.autoformdata[0].editors,"teacherId",{
				link:'../../public/searchUser/searchUser',
				"linkParam":{
					cachename: _that.teacherCacheKey,
					isSingle:true
				}
			}),
			changeEditor(_that.autoformdata[0].editors,"postId",{
				link:'../../public/searchUser/searchUser',
				"linkParam":{
					cachename:_that.postCacheKey,
					isSingle:true,
					searchPos:true,
					label2:'posInstUserName',
					showPeopleNum:false,
					tips:'岗位选择(可选择所有人员岗位)'
				}
			}),
			//培训方式
			_that.om
				.request('tsysdict/getCombobox', {
					code: '38404'
				})
				.then(res => {
					_that.$refs.autoform.changeEditor('title2', 'trainTypeId', 'data', configSelectData(res, 'id', 'dicname'));
					_that.$refs.autoform.initSelectIndex('title2', 'trainTypeId');
				});
			changeEditor(_that.autoformdata[0].editors, 'startTime', 'endField', 'endTime');
			changeEditor(_that.autoformdata[0].editors, 'endTime', 'startField', 'startTime');
			changeEditor(_that.autoformdata[0].editors,"scoreNum","max",10)
			changeEditor(_that.autoformdata[0].editors,"scoreNum","min",0)
			changeEditor(
				_that.autoformdata[0].editors,
				'relevantAbilityIdStr',
				{
					// link: '/pages/public/search/search',
					linkParam: {
						other: JSON.stringify({
							code: '35905'
						}),
						namefield: 'text',
						likefield: 'text',
						initload: true,
						url: 'trainInstYearPlan/getTrainSubjectItemList',
						cachename: _that.relevantAbilityCacheKey
					}
				},
				null
			);
			//负责岗位
			changeEditor(_that.autoformdata[0].editors,'userPostId','isSearchUser',true)
			changeEditor(
				_that.autoformdata[0].editors,
				'userPostId',
				{
					link: '/pages/public/searchUser/searchUser',
					linkParam: {
						other: JSON.stringify({
							containBoard: true
						}),
						// method: 'post',
						label: urlUtil.officePageUrl().namefield,
						label2:'userName',
						searchfield: 'q',
						initload: true,
						paging:true,
						isSingle:true,
						url: urlUtil.officePageUrl().url,
						cachename: _that.postCacheKey
						// placeholder: '请输入机构名称'
					}
				},
				null
			);
			changeEditor(
				_that.autoformdata[0].editors,
				'projectTypeId',
				{
					link: '/pages/public/pageSearch/pageSearch',
					linkParam: {
						other: JSON.stringify({
							code:"38562"
						}),
						namefield:"dicname",
						searchfield:"q",
						initload:true,
						url:"tsysdict/getCombobox",
						cachename: _that.projectCacheKey
					}
				},
				null
			);
			changeEditor(_that.autoformdata[0].editors,"evaluatePostId",{
				link:'../../public/searchUser/searchUser',
				"linkParam":{
					cachename:_that.evaluateUserPostCacheKey,
					isSingle:true,
					searchPos:true,
					label2:'posInstUserName',
					showPeopleNum:false,
					tips:'岗位选择(可选择所有人员岗位)'
				}
			}),
			changeEditor("title2","scoreNum",{
				defaultName:10
			},null,_that.autoformdata)
			_that.$nextTick(function(){
				_that.getMonthList()
			})
		}
	},
	onLoad(e) {
		let _that = this;
		subOnLoad(_that, e, function() {
			if (e.configDataCacheKey) {
				uni.getStorage({
					key: e.configDataCacheKey,
					success: function(res) {
						if (res.data && res.data instanceof Array) {
							_that.autoformdata = res.data;
							Object.assign(_that.initValue, _that.formdata);
							_that.autoformdata[0].order = _that.order;
							_that.initLoad();
						}
						_that.$nextTick(function(){
							let arr = [
								_that.om.request(
									"tsysdict/getCombobox",
									{
										code:'38404'
									}),
								_that.om.request(
									"tsysdict/getCombobox",
									{
										code:'38880'
									}),
								_that.om.request(
									"tsysdict/getCombobox",
									{
										code:'37967'
									})	
							]
							Promise.all(arr).then(res=>{
								_that.$refs.autoform.changeEditor('title2',"trainTypeId",'data',configSelectData(res[0],'id','dicname'))
								_that.$refs.autoform.initSelectIndex('title2',"trainTypeId")
								_that.$refs.autoform.changeEditor('title2',"trainModeId",'data',configSelectData(res[1],'id','dicname'))
								_that.$refs.autoform.initSelectIndex('title2',"trainModeId")
								_that.$refs.autoform.changeEditor('title2',"placeId",'data',configSelectData(res[2],'id','dicname'))
								_that.$refs.autoform.initSelectIndex('title2',"placeId")
							})
						})
					}
				});
			}
		});
	},
	onShow() {
		let _that = this;
		if (_that.$refs.autoform) {
			uni.getStorage({
				key: _that.relevantAbilityCacheKey,
				success: function(res) {
					if (res.data) {
						let d = res.data;
						if (d && d.length > 0) {
							var idStr = joinObjStr(d, 'id');
							var nameStr = joinObjStr(d, 'text');
							var formdata = _that.$refs.autoform.formdata;
							formdata.relevantAbilityIdStr = idStr;
							formdata.relevantAbilityNameStr = nameStr;
							_that.$refs.autoform.changeEditor("title2","relevantAbilityIdStr",{
								initSelect: d
							})
						}
						uni.removeStorage({
							key: _that.relevantAbilityCacheKey
						});
					}
				}
			});
			uni.getStorage({
				key: _that.postCacheKey,
				success: function(res) {
					if (res.data) {
						let d = res.data[0];
						var formdata = _that.$refs.autoform.formdata;
						formdata.postId = d.id;
						formdata.postName = d.posName
						formdata.instId = d.instId
						formdata.instName = d.instName
						uni.removeStorage({
							key: _that.postCacheKey
						});
					}
				}
			});
			uni.getStorage({
				key:_that.monthCacheKey,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.startDateObjId = d.id
						formdata.startDateObjName = d.text
						uni.removeStorage({
							key:_that.monthCacheKey
						})
					}
				}
			})
			uni.getStorage({
				key:_that.monthCacheKey1,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.endDateObjId = d.id
						formdata.endDateObjName = d.text
						uni.removeStorage({
							key:_that.monthCacheKey1
						})
					}
				}
			})
			uni.getStorage({
				key:_that.projectCacheKey,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.projectTypeId = d.id
						formdata.projectTypeName = d.dicname
						uni.removeStorage({
							key:_that.projectCacheKey
						})
					}
				}
			});
			uni.getStorage({
				key: _that.teacherCacheKey,
				success: function(res) {
					if (res.data) {
						let d = res.data[0];
						var formdata = _that.$refs.autoform.formdata;
						_that.verifyTeacher(d.id,d.username)
						uni.removeStorage({
							key: _that.teacherCacheKey
						});
					}
				}
			});
			uni.getStorage({
				key: _that.evaluateUserPostCacheKey,
				success: function(res) {
					if (res.data) {
						let d = res.data[0];
						var formdata = _that.$refs.autoform.formdata;
						formdata.evaluatePostId =d.id
						formdata.evaluatePostName = d.posName
						uni.removeStorage({
							key: _that.evaluateUserPostCacheKey
						});
					}
				}
			});
		}
	}
};
</script>
<style></style>